/**
 * 
 */
package ads.engenharia.ui;

import java.awt.Font;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ads.engenharia.bl.CourseBO;
import ads.engenharia.entity.Course;
import ads.engenharia.iface.IHaveFields;
import javax.swing.border.TitledBorder;
import javax.swing.JMenuItem;
import java.awt.FlowLayout;
import javax.swing.JMenu;

/**
 * @author Lanedraex
 *
 */
public class RegisterCourseView extends JPanel implements IHaveFields {

	JLabel lbl_nameInvalid;
	JLabel lbl_durationInvalid;
	JButton btn_register;
	Thread nameThread;
	Thread durationThread;
	boolean isAllRight;

	public RegisterCourseView() {
		setLayout(null);

		JLabel lbl_name = new JLabel("Nome");
		lbl_name.setBounds(60, 60, 46, 14);
		add(lbl_name);

		txt_name = new JTextField();
		txt_name.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {

				nameThread = new Thread(new Runnable() {
					@Override
					public void run() {
						if (!checkField(txt_name)
								|| txt_name.getText().length() > 50) {

							lbl_nameInvalid.setVisible(true);
							isAllRight = false;
						} else
							isAllRight = true;

					}
				});

				nameThread.run();
				nameThread.interrupt();
			}
		});

		txt_name.setBounds(116, 57, 173, 20);
		add(txt_name);
		txt_name.setColumns(50);

		JLabel lbl_duration = new JLabel("Dura\u00E7\u00E3o");
		lbl_duration.setBounds(60, 88, 46, 14);
		add(lbl_duration);

		txt_duration = new JTextField();
		txt_duration.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {

				durationThread = new Thread(new Runnable() {
					@Override
					public void run() {
						if (!checkField(txt_name)) {

							lbl_durationInvalid.setVisible(true);
							isAllRight = false;
						} else
							isAllRight = true;

					}
				});

				durationThread.run();
				durationThread.interrupt();
			}
		});
		txt_duration.setColumns(10);
		txt_duration.setBounds(116, 85, 173, 20);
		add(txt_duration);

		btn_register = new JButton("Cadastrar");
		btn_register.setBounds(116, 116, 91, 23);
		add(btn_register);
		btn_register.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Course course = new Course();
				CourseBO courseBO = new CourseBO();

				if (!isAllRight)
					return;

				course.name(txt_name.getText());
				course.duration(Integer.parseInt(txt_duration.getText()));
				try {
					courseBO.save(course);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});

		JButton btn_cancel = new JButton("Cancelar");
		btn_cancel.setBounds(217, 116, 91, 23);
		add(btn_cancel);

		lbl_nameInvalid = new JLabel("New label");
		lbl_nameInvalid.setVisible(false);
		lbl_nameInvalid.setLabelFor(txt_name);
		lbl_nameInvalid.setFont(new Font("Tahoma", Font.BOLD, 11));
		lbl_nameInvalid.setBounds(299, 60, 53, 14);
		add(lbl_nameInvalid);

		lbl_durationInvalid = new JLabel("New label");
		lbl_durationInvalid.setVisible(false);
		lbl_durationInvalid.setFont(new Font("Tahoma", Font.BOLD, 11));
		lbl_durationInvalid.setBounds(299, 88, 53, 14);
		add(lbl_durationInvalid);
		
		JPanel pnl_mainMenu = new JPanel();
		pnl_mainMenu.setBounds(10, 0, 434, 20);
		add(pnl_mainMenu);
		pnl_mainMenu.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		
		JMenuBar mb_mainMenu = new JMenuBar();
		pnl_mainMenu.add(mb_mainMenu);
		
		JMenu mn_course = new JMenu("Curso");
		mb_mainMenu.add(mn_course);
		
		JMenuItem mntm_registerCourse = new JMenuItem("Cadastrar");
		mn_course.add(mntm_registerCourse);
		
		JMenuItem mntm_queryCourse = new JMenuItem("Consultar");
		mn_course.add(mntm_queryCourse);
		
		JMenuItem mntm_courseDisciplineRelationship = new JMenuItem("Associar Disciplina");
		mn_course.add(mntm_courseDisciplineRelationship);
		
		JMenu mn_discipline = new JMenu("Disciplina");
		mb_mainMenu.add(mn_discipline);
		
		JMenuItem mntm_registerDiscipline = new JMenuItem("Cadastrar");
		mn_discipline.add(mntm_registerDiscipline);
		
		JMenuItem mntm_queryDiscipline = new JMenuItem("Consultar");
		mn_discipline.add(mntm_queryDiscipline);
		
		JMenuItem mntm_DisciplineProfessorRelationship = new JMenuItem("Associar Disciplina");
		mn_discipline.add(mntm_DisciplineProfessorRelationship);
		
		JMenu mn_professor = new JMenu("Professor");
		mb_mainMenu.add(mn_professor);
		
		JMenuItem mntm_registerProfessor = new JMenuItem("Cadastrar");
		mn_professor.add(mntm_registerProfessor);
		
		JMenuItem mntm_queryProfessor = new JMenuItem("Consultar");
		mn_professor.add(mntm_queryProfessor);
		
		JMenuItem mntm_report = new JMenuItem("Relat\u00F3rio");
		mn_professor.add(mntm_report);
	}

	private static final long serialVersionUID = 1283050030934063651L;
	private JTextField txt_name;
	private JTextField txt_duration;

	@Override
	public boolean checkField(Object field) {
		// if (txt_name.getText().trim().isEmpty()
		// || txt_name.getText().length() > 50) {
		//
		// if (txt_duration.getText().trim().isEmpty()) {
		//
		// return false;
		// }
		// }

		if (field instanceof TextField) {
			if (((TextField) field).getText().trim().isEmpty()) {
				isAllRight = false;
				return false;
			}
		}

		isAllRight = true;
		return true;
	}
}
